package org.leetcode.middle.leetcode15;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {


    public List<List<Integer>> threeSum3_1(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();

        int n = nums.length;

        Arrays.sort(nums);

        int curr = 0;
        int left = 0;
        int right = 0;

        for (int i = curr; i < n; i++) {
            if (nums[i] > 0) {
                return res;
            }

            if (i > 0 && nums[i - 1] == nums[i]) {
                continue;
            }

            left = i + 1;
            right = n - 1;
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    List<Integer> item = new ArrayList<>();
                    item.add(nums[i]);
                    item.add(nums[left]);
                    item.add(nums[right]);
                    res.add(item);

                    while (left < right && nums[left] == nums[left + 1]) {
                        left++;
                    }

                    while (left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }

                    left = left + 1;
                    right = right - 1;
                }
            }
        }

        return res;
    }

    public List<List<Integer>> threeSum3(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();

        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {

            if (nums[i] > 0) {
                return res;
            }
            //去重
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;

            //得出所有当前i可以组成的三数和为0的组合
            while (left < right) {
                int sum = nums[i] + nums[left] + nums[right];

                if (sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {

                    res.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    //去重
                    while (left < right && nums[left] == nums[left + 1]) {
                        left++;
                    }
                    //去重
                    while (left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }

                    left++;
                    right--;
                }
            }
        }
        return res;
    }

    public List<List<Integer>> threeSum2(int[] nums) {

        List<List<Integer>> result = new ArrayList<>();

        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {

            //排序后第一个数字大于0，无法构成三个数相加等于0。
            if (nums[i] > 0) {
                return result;
            }

            //去重，对i进行去重判断
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }

            int left = i + 1;
            int right = nums.length - 1;

            while (right > left) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    //对right进行去重判断
                    while (right > left && nums[right] == nums[right - 1]) {
                        right--;
                    }

                    //对left进行去重判断
                    while (right > left && nums[left] == nums[left + 1]) {
                        left++;
                    }

                    right--;
                    left++;
                }
            }

        }

        return result;
    }


    /**
     * 未完成
     *
     * @param nums
     * @return
     */
    public List<List<Integer>> threeSum(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();
        int one = 0;
        int two = 1;
        int three = 2;

        ArrayList<Integer> resOne = new ArrayList<>();

        while (two < nums.length) {
            if (three == nums.length) {
                one++;
                two++;
                three = two + 1;
            }

            int sum = nums[one] + nums[two];

            if (nums[three] == -sum) {
                resOne.add(nums[one]);
                resOne.add(nums[two]);
                resOne.add(nums[three]);

                res.add(resOne);

                resOne.remove(0);
                resOne.remove(0);
                resOne.remove(0);

            }

            three++;

        }

        return res;
    }


    public static void main(String[] args) {
        ArrayList<Integer> resOne = new ArrayList<>();

        resOne.add(2);
        resOne.add(5);
        resOne.add(7);

        resOne.remove(0);
        resOne.remove(0);
        resOne.remove(0);

        int[] nums = {-1, 0, 1, 2, -1, -4};


        Solution solution = new Solution();
        List<List<Integer>> res = solution.threeSum(nums);


    }
}
